package com.tomtom.amelinium.common;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;

/**
 * The purpose of this class is to read the file/string and divide it into separate lines.
 * 
 * @author Natasza Nowak
 */

public class LineProducer {

	/**
	 * Reads file and returns an array of lines.
	 */
	public ArrayList<String> readLinesFromFile(String name) {

		ArrayList<String> lines = new ArrayList<String>();

		FileInputStream fstream = null;
		DataInputStream in = null;

		try {
			try {
				fstream = new FileInputStream(name);
			} catch (FileNotFoundException e) {
				System.out.println("Error: "+e);
			}
			in = new DataInputStream(fstream);
			BufferedReader br = new BufferedReader(new InputStreamReader(in));

			String line;
			try {
				while ((line = br.readLine()) != null) {
					lines.add(line);
				}
			} catch (IOException e) {
				System.out.println("Error: "+e);
			}
		} finally {
			try {
				in.close();
			} catch (Exception e) {
			}
			try {
				fstream.close();
			} catch (Exception e) {
			}
		}

		return lines;
	}
	
	/**
	 * Divides a string into array of lines.
	 */
	 
	public ArrayList<String> readLinesFromString(String content) {
		return new ArrayList<String>(Arrays.asList(content.split("\n")));
	}

}
